package com.xjz.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjz.common.BaseResponse;
import com.xjz.common.ResultUtils;
import com.xjz.common.enums.ErrorCode;
import com.xjz.exception.BusinessException;
import com.xjz.model.dto.questionsubmit.QuestionSubmitAddRequest;
import com.xjz.model.dto.questionsubmit.QuestionSubmitQueryRequest;
import com.xjz.model.entity.QuestionSubmit;
import com.xjz.model.entity.User;
import com.xjz.model.vo.QuestionSubmitVO;
import com.xjz.service.QuestionSubmitService;
import com.xjz.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * ClassName: QuestionSubmitController
 * Package: com.xjz.controller
 * Description:
 *
 * @Author 许小健
 * @Create 2025/8/17 15:33
 * @Version 1.0
 */
@RestController
@RequestMapping("/question_submit")
@Slf4j
public class QuestionSubmitController {
    @Resource
    private QuestionSubmitService questionSubmitService;

    @Resource
    private UserService userService;

    /**
     * 提交题目
     * @param questionSubmitAddRequest
     * @param request
     * @return
     */
    @PostMapping("/")
    public BaseResponse<Long> doQuestionSubmit(@RequestBody QuestionSubmitAddRequest questionSubmitAddRequest, HttpServletRequest request) {
        if (questionSubmitAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }

        //登录了才能提交
        User loginUser = userService.getLoginUser(request);
        Long questionSubmitId = questionSubmitService.doQuestionSubmit(questionSubmitAddRequest, loginUser);
        return ResultUtils.success(questionSubmitId);
    }

    /**
     * 分页获取题目提交列表
     * @param questionSubmitQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/list/page")
    public BaseResponse<Page<QuestionSubmitVO>> listQuestionSubmitByPage(@RequestBody QuestionSubmitQueryRequest questionSubmitQueryRequest,
                                                                         HttpServletRequest request) {
        if (questionSubmitQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }

        Long current = questionSubmitQueryRequest.getCurrent();
        Long pageSize = questionSubmitQueryRequest.getPageSize();

        User loginUser = userService.getLoginUser(request);
        //从数据库中查询题目提交记录(分页)
        Page<QuestionSubmit> page = questionSubmitService.page(new Page<>(current, pageSize), questionSubmitService.getQueryWrapper(questionSubmitQueryRequest));
        Page<QuestionSubmitVO> questionSubmitVOPage = questionSubmitService.getQuestionSubmitVOPage(page, loginUser);
        return ResultUtils.success(questionSubmitVOPage);
    }
}
